home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / pyshared / AppInstall / DialogComplete.py < prev    next >
Encoding:
Python Source  |  2009-03-31  |  11.1 KB  |  270 lines

  1. # (c) 2005 Canonical, GPL
  2.  
  3. from SimpleGladeApp import SimpleGladeApp
  4.  
  5. import gtk
  6. import gobject
  7. import os
  8.  
  9. from gettext import ngettext
  10. from gettext import gettext as _
  11.  
  12. from Util import *
  13.  
  14. from widgets.AppListView import AppListView
  15.  
  16. class AppListViewComplete(AppListView):
  17.     def __init__(self, cache=None, menu=None, icons=None, executable=False):
  18.         AppListView.__init__(self, style=1)
  19.         self.executable = executable
  20.         self.connect("row-activated", self.on_row_activated)
  21.     def set_executable(self, value):
  22.         self.executable = value
  23.     def on_row_activated(self, treeview_packages, path, column):
  24.         if self.executable != True:
  25.             return False
  26.         if os.getuid() == 0:
  27.             return False
  28.         store = self.get_model()
  29.         treeiter = store.get_iter(path)
  30.         (name, item, popcon) = store[treeiter]
  31.         cmd_parts = []
  32.         command = item.execCmd
  33.         terminal = item.needsTerminal
  34.         if command == "": return
  35.         for part in command.split():
  36.             while True:
  37.                 # two consecutive '%' characters represent an actual '%'
  38.                 if len(part) >= 2 and part[:2] == '%%':
  39.                     cmd_parts.append('%')
  40.                     part = part[2:]
  41.                     continue
  42.                 # we're running the command without any options, so strip 
  43.                 # out placeholders
  44.                 if part[0] == '%': break
  45.                 # if the last part was an actual '%', we don't want to join 
  46.                 # it with a space, so do it by hand
  47.                 if cmd_parts[-1:] == '%':
  48.                     part = '%' + part
  49.                     cmd_parts[-1:] = part
  50.                     break
  51.                 cmd_parts.append(part)
  52.                 break
  53.         
  54.         if terminal:
  55.             command = " ".join(cmd_parts)
  56.             command = "gnome-terminal --command=\"" + command + "\""
  57.             cmd_parts = command.split()
  58.         
  59.         # run program
  60.         os.spawnvp(os.P_NOWAIT, cmd_parts[0], cmd_parts)
  61.  
  62.  
  63. class DialogComplete(SimpleGladeApp):
  64.     def __init__(self, datadir, parent, to_add, to_rm, cache, auto_close=False):
  65.         def add_apps_to_store(apps, store):
  66.             for app in apps:
  67.                 store.append([app.name, app, 0])
  68.         SimpleGladeApp.__init__(self,
  69.                                 path="/usr/share/gnome-app-install/gnome-app-install.glade",
  70.                                 root="window_complete",
  71.                                 domain="gnome-app-install")
  72.         self.cache = cache
  73.         self.auto_close = auto_close
  74.         self.store = gtk.ListStore(gobject.TYPE_STRING,
  75.                                    gobject.TYPE_PYOBJECT,
  76.                                    gobject.TYPE_INT)
  77.         if parent:
  78.             self.window_complete.set_transient_for(parent)
  79.         self.treeview = AppListViewComplete()
  80.         self.scrolledwindow_complete.add(self.treeview)
  81.         self.treeview.set_headers_visible(False)
  82.         self.treeview.set_model(self.store)
  83.         self.treeview.show()
  84.         # Identify failed and exectuable applications and extras
  85.         # (stuff without execCmd lines like codecs)
  86.         failed_apps = []
  87.         failed_extras = []
  88.         installed_apps = []
  89.         installed_extras = []  
  90.         self.install_failures = False
  91.         for app in to_add:
  92.             pkg = app.pkgname
  93.             available = cache.has_key(pkg)
  94.             installed = available and cache[pkg].isInstalled
  95.             if not available:
  96.                 if app.execCmd != "":
  97.                     failed_apps.append(app)
  98.                 else:
  99.                     failed_extras.append(app)
  100.             elif not installed:
  101.                  if app.execCmd != "":
  102.                     failed_apps.append(app)
  103.                  else:
  104.                     failed_extras.append(app)
  105.             elif installed and app.execCmd != '':
  106.                 installed_apps.append(app)
  107.             elif installed and app.execCmd == '':
  108.                 installed_extras.append(app)
  109.         for app in to_rm:
  110.             pkg = app.pkgname
  111.             if self.cache.has_key(pkg) and self.cache[pkg].isInstalled:
  112.                 if app.execCmd != "":
  113.                     failed_apps.append(app)
  114.                 else:
  115.                     failed_extras.append(app)
  116.         # record if anything failed (for auto_close)
  117.         if failed_extras or failed_apps:
  118.                 self.install_failures = True
  119.         # Connect the signals
  120.         self.button_complete_retry.connect("clicked",
  121.                                            self.on_button_retry_clicked)
  122.         self.button_complete_more.connect("clicked",
  123.                                           self.on_button_more_clicked)
  124.         self.button_complete_close.connect("clicked",
  125.                                            self.on_button_close_clicked)
  126.         self.window_complete.connect("delete-event",
  127.                                      self.on_delete)
  128.         # Adjust the dialog text
  129.         if (len(failed_apps) > 0 or len(failed_extras) > 0) and len(to_rm) == 0:
  130.             if len(failed_extras) > 0:
  131.                 header = _("Software installation failed")
  132.                 body = _("There has been a problem during the installation "
  133.                          "of the following pieces of software.")
  134.                 self.button_complete_more.set_label(_("Add/Remove More Software"))
  135.             else:
  136.                 header = _("Application installation failed")
  137.                 body = _("There has been a problem during the installation "
  138.                          "of the following applications.")
  139.             add_apps_to_store(failed_apps, self.store)
  140.             add_apps_to_store(failed_extras, self.store)
  141.             self.image_complete_icon.set_from_stock(gtk.STOCK_DIALOG_ERROR,
  142.                                                     gtk.ICON_SIZE_DIALOG)
  143.             self.button_complete_retry.show()
  144.         elif (len(failed_apps) > 0 or len(failed_extras)> 0) and\
  145.              len(to_add) == 0:
  146.             if len(failed_extras) > 0:
  147.                 header = _("Software could not be removed")
  148.                 body = _("There has been a problem during the removal "
  149.                          "of the following pieces of software.")
  150.                 self.button_complete_more.set_label(_("Add/Remove More Software"))
  151.             else:
  152.                 header = _("Not all applications could be removed")
  153.                 body = _("There has been a problem during the removal "
  154.                          "of the following applications.")
  155.             add_apps_to_store(failed_apps, self.store)
  156.             add_apps_to_store(failed_extras, self.store)
  157.             self.image_complete_icon.set_from_stock(gtk.STOCK_DIALOG_ERROR,
  158.                                                     gtk.ICON_SIZE_DIALOG)
  159.             self.button_complete_retry.show()
  160.         elif len(failed_extras) > 0 or len(failed_apps) > 0:
  161.             #FIXME: perhaps separate widgets would make more sence
  162.             if len(failed_extras) > 0:
  163.                 header = _("Installation and removal of software failed")
  164.                 body = _("There has been a problem during the installation or "
  165.                          "removal of the following pieces of software.")
  166.                 self.button_complete_more.set_label(_("Add/Remove More Software"))
  167.             else:
  168.                 header = _("Installation and removal of applications failed")
  169.                 body = _("There has been a problem during the installation or "
  170.                          "removal of the following applications.")
  171.             add_apps_to_store(failed_apps, self.store)
  172.             add_apps_to_store(failed_extras, self.store)
  173.             self.button_complete_retry.show()
  174.             self.image_complete_icon.set_from_stock(gtk.STOCK_DIALOG_ERROR,
  175.                                                     gtk.ICON_SIZE_DIALOG)
  176.         elif len(installed_apps) > 0:
  177.             header = ngettext(_("New application has been installed"),
  178.                               _("New applications have been installed"),
  179.                               len(installed_apps))
  180.             add_apps_to_store(installed_apps, self.store)
  181.             # we do not support launching apps when runing as root
  182.             if os.getuid() == 0:
  183.                 body = _("To start a newly installed application, "
  184.                          "choose it from the applications menu.")
  185.             else:
  186.                 body = _("To start a newly installed application "
  187.                          "double click on it.")
  188.                 self.treeview.set_executable(True)
  189.         elif len(installed_extras) > 0:
  190.             header = _("Software has been installed successfully")
  191.             body = _("Do you want to install or remove further " \
  192.                      "software?")
  193.             self.scrolledwindow_complete.hide()
  194.             self.button_complete_more.set_label(_("Add/Remove More Software"))
  195.         else:
  196.             header = _("Applications have been removed successfully")
  197.             body = _("Do you want to install or remove further " \
  198.                      "applications?")
  199.             self.scrolledwindow_complete.hide()
  200.         self.label_complete.set_markup("<b><big>%s</big></b>\n\n%s" % (header,
  201.                                                                        body))
  202.  
  203.     def run(self):
  204.         if (self.auto_close and self.install_failures == False):
  205.             return gtk.RESPONSE_CLOSE
  206.         self.window_complete.show()
  207.         gtk.main()
  208.         return self._response
  209.     def _finish(self):
  210.         self.window_complete.hide()
  211.         gtk.main_quit()
  212.     def on_button_close_clicked(self, button):
  213.         self._response = gtk.RESPONSE_CLOSE
  214.         self._finish()
  215.     def on_button_retry_clicked(self, button):
  216.         self._response = 1
  217.         self._finish()
  218.     def on_button_more_clicked(self, button):
  219.         self._response = 2
  220.         self._finish()
  221.     def on_delete(self, widget, event):
  222.         self._response = gtk.RESPONSE_CLOSE
  223.         self._finish()
  224.         return gtk.TRUE
  225.  
  226. if __name__ == "__main__":
  227.     from AppInstall.CoreMenu import CoreApplicationMenu
  228.     from AppInstall.DialogComplete import DialogComplete
  229.     import gtk
  230.     import apt
  231.     import cPickle
  232.     import pdb
  233.  
  234.     cache = apt.Cache()
  235.     progress = apt.progress.OpTextProgress()
  236.     to_add = []
  237.  
  238.     datadir = "/usr/share/app-install"
  239.     desktopdir = "/usr/share/app-install"
  240.     cachedir = "/var/cache/app-install"
  241.  
  242.     treeview_categories = gtk.TreeView()
  243.     treeview_packages = gtk.TreeView()
  244.  
  245.     menu = CoreApplicationMenu(datadir)
  246.     menu.pickle = cPickle.load(open("/var/cache/app-install/menu.p"))
  247.  
  248.     available = []
  249.     available_extras = []
  250.     installed = []
  251.     installed_extras = []
  252.  
  253.     print menu.pickle.keys()
  254.     for app in menu.pickle[menu.pickle.keys()[0]]:
  255.         if cache.has_key(app.pkgname) and cache[app.pkgname].isInstalled:
  256.             if app.execCmd:
  257.                 installed.append(app)
  258.             else:
  259.                 installed_extras.append(app)
  260.         else:
  261.             if app.execCmd:
  262.                 available.append(app)
  263.             else:
  264.                 available_extras.append(app)
  265.     to_rm = []
  266.     while True:
  267.         pdb.set_trace()
  268.         dia = DialogComplete(datadir, None, to_add, to_rm, cache)
  269.         dia.run()
  270.